package oracle.apps.financials.collection.listener;

import oracle.apps.financials.collection.R;
import oracle.apps.financials.collection.po.Customer;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.Toast;

public class NotePopupWindowListener extends BaseOnClickListener {

	private EditText noteText;

	public NotePopupWindowListener(Context context, Customer customer) {
		super(context, R.layout.note, customer);
	}

	@Override
	public void onClick(View parent) {

		if (this.popupWindow == null) {
			initPopupWindow();
		}

		popupWindow.showAsDropDown(parent, -10, 2);
	}

	public void initPopupWindow() {
		dismissPopupWindow();
		this.popupWindow = new PopupWindow(this.popupWindowView, 250, 250);

		noteText = (EditText) popupWindowView.findViewById(R.id.customer_note);
		noteText.setText(this.customer.getNote());
		popupWindow.setTouchInterceptor(new OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
					String note = noteText.getText().toString();
					NotePopupWindowListener.this.customer.setNote(note);
					Toast.makeText(context, "You entered note:" + note, Toast.LENGTH_SHORT).show();
					popupWindow.dismiss();
					return true;
				}
				return false;
			}

		});

		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
	}

}
